<template>
	<view class="content">
		<view v-if="pageData.startUp == 1">
			<view class="advertising">
				<image :src="imgUrls(pageData.start_img)" mode="aspectFill"></image>
			</view>
			<view class="fiexd_bottom">
				<view v-show="isShow" class="indexBtn" @click="toPageUrl">
					<text class="h6 white">跳过</text>
					<text>{{cudTime}}</text>
					<text class="h6 white">S</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref, inject } from 'vue'
	import { onLoad, onShow } from '@dcloudio/uni-app'
	import Http from '@/common/request.js'
	import { usePiniaStore } from '@/store/pinia.js'

	const Pinia = usePiniaStore()
	const { Toast, imgUrls, pageHome } = inject('Utils')
	const isShow = ref(false)
	const cudTime = ref(5)
	const dtime = ref(null)
	const Opt = ref('')
	const pageData = ref('')

	onLoad((options) => {
		if (options.pageType || options.share_v_id) Opt.value = options;
		if (options.scene) optScene(options.scene);

		// #ifdef MP-WEIXIN
		getWXOpenid();
		Pinia.Location();
		// #endif

		Pinia.essayInfo(); // 红娘信息
	})

	onShow(() => {
		startUp()
	})



	const startUp = () => {
		Http.post({
			url: '/zzj_singleSaleApi/startUp',
		}).then(res => {
			if (res.errcode != '100') return Toast(res.msg);
			const result = res.data;
			pageData.value = result;
			Pinia.$patch((state) => {
				state.share = result.share
				state.logo = result.logo_img
				state.nameDefinition = result.nameDefinition
				state.withdrawal_rules = result.withdrawal_rules
				state.title = result.title
				state.service_tel = result.service_tel
			})
			// #ifdef H5
			Pinia.$patch((state) => {
				state.vid = res.v_id
				state.openid = res.open_id
			})
			// #endif
			if (result.startUp != 1) return toPageUrl();
			cudTime.value = result.countdown;
			dtime.value = setInterval(() => {
				if (cudTime.value > 1) {
					cudTime.value--;
				} else {
					clearInterval(dtime.value);
					toPageUrl()
				}
			}, 1000)
		})
	}

	const toPageUrl = () => {
		let url;
		let Result = Opt.value;
		if (Result.share_v_id) Pinia.$patch((state) => state.share_v_id = Result.share_v_id)
		if (!Result || !Result.pageType) return pageHome();
		if (Result.pageType == 2) { // 人物详情
			url = `/pages/Details/Details?id=${Result.artId}&fromShare=1`
		} else if (Result.pageType == 3) { // 活动详情
			url = `/pages/EventsDetails/EventsDetails?event_id=${Result.artId}&fromShare=1`
		} else if (Result.pageType == 4) { // 活动列表
			url = `/pages/EventsClass/EventsClass?cate_id=${Result.artId}&fromShare=1`
		}
		uni.reLaunch({
			url
		});
	}



	const optScene = (sceneData) => {
		let sceneInfo = decodeURIComponent(sceneData).split('&');
		let sceneType = {};
		sceneInfo.forEach(item => {
			let pram = item.split("=")
			sceneType[pram[0]] = pram[1]
		})
		let opt = {
			artId: '',
			pageType: '',
			share_v_id: '',
		}
		if (sceneType.gid) opt.artId = sceneType.gid;
		if (sceneType.artId) opt.artId = sceneType.artId;
		if (sceneType.p && sceneType.p != 0) opt.pageType = sceneType.p;
		if (sceneType.sid) opt.share_v_id = sceneType.sid;
		Opt.value = opt;
	}


	const getWXOpenid = () => {
		uni.getProvider({
			service: 'oauth',
			success(res) {
				uni.login({
					provider: res.provider[0],
					success(req) {
						Http.get({
							url: '/zzj_singleSaleApi/getOpenId',
							data: {
								js_code: req.code,
								share_v_id: Pinia.share_v_id
							}
						}).then(res2 => {
							if (res2.code != '100') return Toast(res2.msg);
							const Result = res2.data;
							Pinia.$patch((state) => {
								state.vid = Result.vid;
								state.openid = Result.openid;
								state.session_key = Result.session_key;
								state.userInfo = Result.userInfo;
							})
						})
					}
				})
			}

		})
	}
</script>

<style lang="scss" scoped>
	.content {
		overflow: hidden;
		display: flex;
		background: #FFFFFF;
		height: 100vh;

		.advertising {
			width: 100%;
			height: 100%;
			position: absolute;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.fiexd_bottom {
			width: 100%;
			height: 300rpx;
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			// background: #FFFFFF;
			z-index: 5;

			image {
				width: 200rpx;
				height: 200rpx;
				position: absolute;
				left: 50%;
				margin-left: -100rpx;
				top: 50%;
				margin-top: -100rpx;
			}

			.indexBtn {
				padding: 0 20rpx;
				line-height: 50rpx;
				border-radius: 64rpx;
				border: 1rpx solid #999999;
				color: #FFFFFF;
				position: absolute;
				right: 30rpx;
				top: 40%;

				text {
					color: #FFFFFF;
					font-size: 24rpx;
					padding: 0 4rpx;
				}
			}
		}

	}
</style>